package uit.mmt02.dataAccess;

import java.util.ArrayList;
import java.util.List;

import uit.mmt02.doituong.EmailLoginManager;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.NetworkInfo.State;
import android.util.Log;

public class DB_EmailLoginManager_Adapter 
{
	public static final String DB_NAME = "EmailLoginManager";
	public static final String DB_TABLE = "EmailLogin";
	public static final int DB_VERSION = 1;
	
	//cac truong trong table EmailLogin
	public static final String ID = "number";
	public static final String USERNAME = "username";
	public static final String PASSWORD = "password";
	public static final String TONGMAIL = "tongmail";
	public static final String NEWMAIL = "newmail";
	public static final String MYKEY = "Mykey";
	public static final String MY_PRIVATE_RSA_KEY = "MyPrivateRSAkey";
	public static final String MY_PUBLIC_RSA_KEY = "MyPublicRSAkey";
	
	String sql;
	String tag = "myDB";
	
	Context context;
	SQLiteDatabase mDB;
	DBHelper dbHelper;
	
	public DB_EmailLoginManager_Adapter(Context context)
	{
		this.context = context;
	}
	
	private class DBHelper extends SQLiteOpenHelper
	{

		public DBHelper(Context context, String name, CursorFactory factory,
				int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}
		
		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			try
			{
			sql = "create table EmailLogin " +
					"(number integer PRIMARY KEY autoincrement, " +
					"username text, password text, " +
					"tongmail integer, newmail integer, " +
					"Mykey text, MyPrivateRSAkey text, MyPublicRSAkey text);";
			Log.v(tag, " khoi tao DB");
			db.execSQL(sql);
			}
			catch (Exception e) {
				// TODO: handle exception
				Log.v(tag, "Loi tao table EmailLogin " +
						e.toString());
			}
		}
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			sql = "DROP TABLE IF EXISTS EmailLogin";
			db.execSQL(sql);
			Log.v(tag, "onupgrade");
			
		}
		
	} //end DBHelper
	
	public DB_EmailLoginManager_Adapter open()
	{
		dbHelper = new DBHelper(context, DB_NAME, null, DB_VERSION);
		mDB = dbHelper.getWritableDatabase();
		Log.i(tag, " myDB Open");
		return this;
	}
	
	public void close()
	{
		dbHelper.close();
		Log.i(tag, " Close mDB");
	}
	public long insert(EmailLoginManager newEmaillogin)
	{
		try
		{
		ContentValues cv = new ContentValues();
		cv.put(USERNAME, newEmaillogin.getUsername());
		cv.put(PASSWORD, newEmaillogin.getPassword());
		cv.put(TONGMAIL, newEmaillogin.getTongmail());
		cv.put(NEWMAIL, newEmaillogin.getNewmail());
		cv.put(MYKEY, newEmaillogin.getMykey());
		cv.put(MY_PRIVATE_RSA_KEY, newEmaillogin.getMyPrivateRSAkey());
		cv.put(MY_PUBLIC_RSA_KEY, newEmaillogin.getMyPublicRSAkey());
		Log.i(tag, " insert  thanh cong");
		return mDB.insert(DB_TABLE, null, cv);
		}
		catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "Loi insert  " +
					e.toString());
			return 0;
		}
	} //end insert
	
	public Cursor get_email_tongmail_newmail()
	{
		Log.i(tag, " get CSDL");
		return mDB.query(DB_TABLE, new String[]{USERNAME,PASSWORD, TONGMAIL, NEWMAIL}, null, null, null, null, null);
	}
	
	public Cursor get_byEmail(String username)
	{
		Log.i(tag, " get 2");
		Cursor mCursor = mDB.query(true, DB_TABLE, new String[]{USERNAME,TONGMAIL,NEWMAIL}, USERNAME + "="+username, null, null, null, null, null);
		if(mCursor !=null)
		{
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	public boolean removeALL()
	{
		Log.i(tag, "remove username all");
		return mDB.delete(DB_TABLE, null, null) > 0;
	}
	public int remove_by_username(String username)
	{
		int t = 0;
		try
		{
			t = mDB.delete(DB_TABLE, USERNAME + "= " +
					"\"" +
					username + 
					"\"", null);
			Log.v(tag, "delete thanh cong  "+ t+"   " + username );
		}catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "Loi delete  " +
					e.toString());
		}
		return t;
		
	}
	

	
	public boolean autoCSDL()
	{
		removeALL();
		EmailLoginManager em = new EmailLoginManager("sonntuit@gmail.com", "sacrifices", 110, 12);
		EmailLoginManager em1 = new EmailLoginManager("vipboy286@yahoo.com", "sacrifices", 5, 0);
		EmailLoginManager em2 = new EmailLoginManager("thaithao07520323@yahoo.com", "abc@123456", 120, 11);
		EmailLoginManager em3 = new EmailLoginManager("u2@ctya.com", "123456", 120, 11);
		EmailLoginManager em4 = new EmailLoginManager("thaithao07520323@hotmail.com", "abc@123456", 121, 12);
		EmailLoginManager em5 = new EmailLoginManager("123@gmail.com", "xxx", 122, 13);
		EmailLoginManager em6 = new EmailLoginManager("LVT@hotmail.com", "xxx", 123, 14);
		EmailLoginManager em7 = new EmailLoginManager("TTTT@gmail.com", "xxx", 124, 15);
		EmailLoginManager em8 = new EmailLoginManager("Son@uit.mmt02", "xxx", 125, 16);
		insert(em);
		insert(em1);
		insert(em2);
		insert(em3);
		insert(em4);
		insert(em5);
		insert(em6);
		insert(em7);
		insert(em8);
		Log.i(tag, "auto khoi tao CSDL");
		return true;

	}
	
	public List<EmailLoginManager> convertCursorToList(Cursor mCursor)
	{
		List<EmailLoginManager>list = new ArrayList<EmailLoginManager>();
		try
		{
			if(mCursor.moveToFirst())
			{
				do {
					String username = mCursor.getString(0);
					String password = mCursor.getString(1);
//					int tongmail = mCursor.getInt(2);
					int newmail = mCursor.getInt(3);
					EmailLoginManager email = new EmailLoginManager();

					email.setUsername(username);
					email.setPassword(password);
//					email.setTongmail(tongmail);
					email.setNewmail(newmail);
					list.add(email);
					
				} while (mCursor.moveToNext());
			}
			return list;
		}catch (Exception e) {
			// TODO: handle exception
			Log.i(tag, " loi convert");
			return null;
		}
	}
	
	public String db_getpassword(String username)
	{
		if(!mDB.isOpen())
			open();
		String password = null;
		Cursor mCursor = mDB.query(true, DB_TABLE, new String[]{PASSWORD}, USERNAME + " = " + username, null, null, null, null, null);
		if(mCursor.getCount() > 0)
		{
			password  = mCursor.getString(0);
		}
		mCursor.close();
		mDB.close();
		return password;
	}
	
	

}
